package org.sippr.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    public static String writeValueAsString(Object value) {
        try {
            // 创建 ObjectMapper 对象，用于 JSON 序列化和反序列化
            ObjectMapper objectMapper = new ObjectMapper();

            // 将 Person 对象序列化为 JSON 字符串
            String json = objectMapper.writeValueAsString(value);
            return json;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    public static <T> T parseJson(String jsonString, Class<T> clazz) {
        try {
            // 创建 ObjectMapper 对象
            ObjectMapper objectMapper = new ObjectMapper();

            // 将 JSON 字符串解析为指定类型的对象
            return objectMapper.readValue(jsonString, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
